<?php

class User extends SActiveRecord
{
    public static $objects;
    public static $relationships = array(
        'statuses' => array('assoc_type' => 'has_many', 'foreign_key' => 'sender_id'),
        
        'favorites' => array('assoc_type' => 'many_to_many', 'join_table' => 'favorites',
                             'class_name' => 'Status'),
        'friends' => array('assoc_type' => 'many_to_many', 'join_table' => 'friendships',
                           'class_name' => 'User', 'association_foreign_key' => 'friend_id')
    );
    
    public static function authenticate($login)
    {
        try {
            $user = User::$objects->get('name = ?', array($login));
            return array($user, $user->pwd);
        } catch (SRecordNotFound $e) {
            return false;
        }
    }
    
    public function validate()
    {
        $this->validate_presence_of(array('name', 'email', 'pwd'),
                                    array('message' => 'est obligatoire'));
        $this->validate_format_of('name', array('pattern' => 'alphanum',
                                                'message' => 'doit être alphanumérique'));
        $this->validate_format_of('email', array('pattern' => 'email',
                                                 'message' => 'ne semble pas être valide'));
    }
    
    public function serializable_form()
    {
        return parent::serializable_form(array('exclude' => array('pwd')));
    }
}

?>
